#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <math.h>

#include "include/GUInterface.h"
#include "include/Jeu.h"
#include "include/Monde.h"
#include "include/Territoire.h"
#include "include/Partie.h"
#include "include/MissionStrategy.h"
#include "include/Joueur.h"

using namespace std;
/*
  Fonts:
  ------
  GLUT_BITMAP_9_BY_15
  GLUT_BITMAP_8_BY_13
  GLUT_BITMAP_TIMES_ROMAN_10
  GLUT_BITMAP_TIMES_ROMAN_24
  GLUT_BITMAP_HELVETICA_10
  GLUT_BITMAP_HELVETICA_12
  GLUT_BITMAP_HELVETICA_18
*/
GUInterface::GUInterface()
   : positionNomJoueur(new Point3D(0.0f, -0.0f)), window_width(0), window_height(0)
{
   showingHelp = true;
   showingNomJoueur = false;
}

GUInterface::~GUInterface()
{
   //printf("Destruction de GUInterface !\n");
}

void GUInterface::setWindowDimension(int _width, int _height)
{
   window_width=_width;
   window_height=_height;
}

void printstring(float x, float y, void * font, const char * string, int max_width=600)
{
   int len, i;
   int cur_width = 0;
   int nbSauts = 0;
   i = 0;
   len = (int) strlen(string);

   glDisable(GL_TEXTURE_2D);
   glDisable(GL_DEPTH_TEST);
   glRasterPos3f(x, y, -0.01f);

   do
   {
      for (; i < len && cur_width < max_width; i++)
      {
         glutBitmapCharacter(font, string[i]);
         cur_width += glutBitmapWidth(font, string[i]);
      }
      if ( i < len )
      {
         nbSauts++;
         glRasterPos3f(x, y+(0.05f*nbSauts), -0.01f);
         cur_width = 0;
      }
   } while ( i < len );
}

void GUInterface::printHelpDialog()
{
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

   glPushMatrix();
   glLoadIdentity();
   gluLookAt (0, 0, -1,
              0, 0, 0,
              0, -1, 0);

   glColor4f(0.0, 0.0, 0.0, 0.6);
   glRectf(-0.65f, -0.65f, 0.65f, 0.65f);
   glDisable(GL_BLEND);

   glColor3f(0.0f, 1.0f, 0.0f);
   printstring(-0.6f, -0.6f, GLUT_BITMAP_TIMES_ROMAN_24, "Raccourcis clavier :");
   printstring(-0.6f, -0.55f, GLUT_BITMAP_HELVETICA_18, "h - Afficher/Cacher l'aide");
   printstring(-0.6f, -0.5f, GLUT_BITMAP_HELVETICA_18, "Echap - Quitter le jeu");
   printstring(-0.6f, -0.45f, GLUT_BITMAP_HELVETICA_18, "d - Naviguer dans les modes d'affichages");
   printstring(-0.6f, -0.4f, GLUT_BITMAP_HELVETICA_18, "a - Activer/Desactiver la couche atmospherique");
   printstring(-0.6f, -0.35f, GLUT_BITMAP_HELVETICA_18, "m - Activer/Desactiver l'animation de la mer");
   printstring(-0.6f, -0.30f, GLUT_BITMAP_HELVETICA_18, "MouseWheel UP/DOWN - Zoom IN/OUT");
   printstring(-0.6f, -0.25f, GLUT_BITMAP_HELVETICA_18, "Shift + MouseWheel UP/DOWN - View UP/DOWN");
   printstring(-0.6f, -0.20f, GLUT_BITMAP_HELVETICA_18, "Ctrl + MouseWheel UP/DOWN - Rotate View LEFT/RIGHT");
   printstring(-0.6f, -0.15f, GLUT_BITMAP_HELVETICA_18, "Space - Reset view to default");

   glColor3f(1.0f, 1.0f, 0.0f);
   printstring(-0.6f, -0.05f, GLUT_BITMAP_TIMES_ROMAN_24, "Votre mission :");
   printstring(-0.6f, -0.0f, GLUT_BITMAP_HELVETICA_18, Jeu::jeu->getPartie()->getCurrentPlayer()->getMission()->getDescription());

   glPopMatrix();
}

void GUInterface::printHelpMessage()
{
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

   glPushMatrix();
   glLoadIdentity();
   gluLookAt (0, 0, -1,
              0, 0, 0,
              0, -1, 0);

   glColor4f(0.0, 0.0, 0.0, 0.5);
   glRectf(-0.65f, 0.45f, 0.65f, 0.6f);
   glDisable(GL_BLEND);

   glColor3f(1.0f, 1.0f, 0.0f);
   printstring(-0.6f, 0.49f, GLUT_BITMAP_HELVETICA_18, messageInfo.c_str());

   glPopMatrix();   
}

void GUInterface::printStatusBar()
{
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

   glPushMatrix();
   glLoadIdentity();
   gluLookAt (0, 0, -1,
              0, 0, 0,
              0, -1, 0);

   glPushMatrix();
   glTranslatef(-1.0, 0.66, 0.0);
   glColor4f(0.0, 0.0, 0.0, 1.0);
   glRectf(0, 0, 2.0, 0.043);
   glDisable(GL_BLEND);

   glPopMatrix();

   glColor4f(0, 1.0, 0, 1.0);
   printstring(-0.65, 0.68, GLUT_BITMAP_8_BY_13, messageStatus.c_str());

   glPopMatrix();   
}

void GUInterface::printJoueursLegend(vector<Joueur *> * _joueurs)
{
   glPushMatrix();
   glLoadIdentity();
   gluLookAt (0, 0, -1,
              0, 0, 0,
              0, -1, 0);
   int i=0;
   vector<Joueur*>::iterator joueur_ite;
   for(joueur_ite = _joueurs->begin();
       joueur_ite != _joueurs->end();
       joueur_ite++)
   {
      ostringstream str;
      str << Jeu::jeu->getMonde()->countTerritoiresJoueur( (*joueur_ite) );

      string label((*joueur_ite)->getNom());
      label += " : ";
      label += str.str();
      label += " territoires";

      glPushMatrix();
      glTranslatef(-.7f, (i*0.04f)-0.7f, 0.0f);
      glColor3fv( (const float *)(*joueur_ite)->getCouleur() );
      glRectf(0.0f, 0.0f, .1f, .037f);
      glPopMatrix();
      
      glColor3f(0.0f, 0.0f, 0.0f);
      printstring(-0.59f, (i*0.04f)-0.675f, GLUT_BITMAP_8_BY_13, label.c_str());
      i++;
   }
   glPopMatrix();
}

void GUInterface::printNomJoueur()
{
   glPushMatrix();
   glLoadIdentity();
   gluLookAt (0, 0, -1,
              0, 0, 0,
              0, -1, 0);
   
   string message("JOUEUR: ");
   message += Jeu::jeu->getPartie()->getCurrentPlayer()->getNom();

   glColor3f(1.0f, 0.0f, 0.0f);
   if ( positionNomJoueur )
      printstring(positionNomJoueur->x(), 
                  positionNomJoueur->y(), 
                  GLUT_BITMAP_TIMES_ROMAN_24, 
                  message.c_str());

   glPopMatrix();
}
